package api

import (
	"fmt"
	"getaway/basic/config"
	__ "getaway/basic/proto/baseproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"getaway/untils"
	"github.com/gin-gonic/gin"
	"mime/multipart"
)

// PlatformAdd
// @Summary      平台版本添加
// @Description  平台版本添加
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.PlatformAddReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/platformAdd [post]
// @Security TokenAuth
func PlatformAdd(c *gin.Context) {
	var req request.PlatformAddReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	/*way, err := untils.UploadOneCompressedFile(c, req.FileUrl)
	if err != nil {
		response.ParameterError(c, "上传文件失败", err.Error())
		return
	}*/
	// 保存到数据库
	_, err := config.BaseClient.PlatformAdd(c, &__.PlatformAddReq{
		PlatformName:    req.PlatformName,
		PlatformVersion: req.PlatformVersion,
		VersionCode:     req.VersionCode,
		UserId:          c.GetInt64("userId"),
		StartUpdateTime: req.StartUpdateTime,
	})

	if err != nil {
		response.SrvError(c, "平台上传失败", err.Error())
		return
	}
	response.Success(c, "平台上传成功", nil)

}

// PlatformInfoList
// @Summary      平台版本列表
// @Description  平台版本列表
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/platformInfoList [get]
// @Security TokenAuth
func PlatformInfoList(c *gin.Context) {
	platformList, err := config.BaseClient.PlatformList(c, &__.PlatformListReq{})
	if err != nil {
		response.SrvError(c, "平台列表获取失败", err.Error())
		return
	}
	response.Success(c, "平台列表获取成功", platformList)
}

// PlatformUpdate
// @Summary      平台版本修改
// @Description  平台版本修改
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.PlatformAddReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/platformUpdate [post]
// @Security TokenAuth
func PlatformUpdate(c *gin.Context) {
	var req request.PlatformUpdateReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	if req.FileUrl != "" {
		// 修改文件
		way, err := untils.UploadOneCompressedFile(c, req.FileUrl)
		if err != nil {
			response.ParameterError(c, "上传文件失败", err.Error())
			return
		}
		// 保存到数据库
		_, err = config.BaseClient.PlatformUpdate(c, &__.PlatformUpdateReq{
			PlatformName:    req.PlatformName,
			PlatformVersion: req.PlatformVersion,
			VersionCode:     req.VersionCode,
			UserId:          c.GetInt64("userId"),
			StartUpdateTime: req.StartUpdateTime,
			Id:              req.Id,
		})
		if err != nil {
			response.SrvError(c, "平台更新失败", err.Error())
			return
		}
		response.Success(c, "平台更新成功", way)
	} else {
		// 保存到数据库
		_, err := config.BaseClient.PlatformUpdateWithoutFile(c, &__.PlatformUpdateWithoutFileReq{
			PlatformName:    req.PlatformName,
			PlatformVersion: req.PlatformVersion,
			VersionCode:     req.VersionCode,
			UserId:          c.GetInt64("userId"),
			StartUpdateTime: req.StartUpdateTime,
			Id:              req.Id,
		})
		if err != nil {
			response.SrvError(c, "平台更新失败", err.Error())
			return
		}
		response.Success(c, "平台更新成功", nil)
	}

}

// ApplicationAdd
// @Summary      应用版本添加
// @Description  应用版本添加
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Accept       multipart/form-data
// @Param        image formData file true "应用文件"
// @Param        base   body    request.ApplicationAddReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/applicationAdd [post]
// @Security TokenAuth
func ApplicationAdd(c *gin.Context) {
	var req request.ApplicationAddReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	way, err := untils.UploadOneCompressedFile(c, req.FileUrl)
	if err != nil {
		response.ParameterError(c, "上传文件失败", err.Error())
		return
	}

	file, err := c.FormFile("image")
	if err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	get, err := file.Open()
	if err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	url, err := untils.QiNiuUpload(get, file)
	if err != nil {
		response.ParameterError(c, "上传文件失败", err.Error())
		return
	}

	// 保存到数据库
	_, err = config.BaseClient.ApplicationAdd(c, &__.ApplicationAddReq{
		AppName:         req.AppName,
		AppSortOrder:    int32(req.AppSortOrder),
		AppIcon:         url,
		AppPackageName:  req.AppPackageName,
		AppStatus:       int32(req.AppStatus),
		UserId:          c.GetInt64("userId"),
		StartUpdateTime: req.StartUpdateTime,
	})
	if err != nil {
		response.SrvError(c, "应用添加失败", err.Error())
		return
	}
	response.Success(c, "应用添加成功", fmt.Sprintf("图片路径:%s,安装包路径:%s", url, way))
}

// ApplicationUpdate
// @Summary      应用版本修改
// @Description  应用版本修改
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Accept      multipart/form-data
// @Param        image formData file false "应用文件"
// @Param        base   body    request.ApplicationUpdateReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/applicationUpdate [post]
// @Security TokenAuth
func ApplicationUpdate(c *gin.Context) {

	// 先获取文件并检查是否为空
	file, err := c.FormFile("image")

	if err != nil {
		if err.Error() == "http: no such file" {
			fmt.Println("文件为空", file)
			// 文件不存在时继续向下执行
		} else {
			// 处理其他类型的错误
			response.ParameterError(c, "参数错误", err.Error())
			return
		}
	}

	/*fmt.Println("文件为空", file)
	var req request.ApplicationUpdateReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	fmt.Println(req)*/
	/*// 文件不为空且FileUrl不为空的情况
	if file != nil && file.Filename != "" && req.FileUrl != "" {
		// 修改文件
		url, err := untils.UploadOneCompressedFile(c, req.FileUrl)
		if err != nil {
			response.ParameterError(c, "上传安装包文件失败", err.Error())
			return
		}

		get, err := file.Open()
		if err != nil {
			response.ParameterError(c, "打开文件失败", err.Error())
			return
		}
		defer get.Close() // 确保文件被关闭

		way, err := untils.QiNiuUpload(get, file)
		if err != nil {
			response.ParameterError(c, "上传文件失败", err.Error())
			return
		}

		_, err = config.BaseClient.ApplicationUpdate(c, &__.ApplicationUpdateReq{
			Id:              req.Id,
			AppName:         req.AppName,
			AppSortOrder:    int32(req.AppSortOrder),
			AppIcon:         way,
			AppPackageName:  req.AppPackageName,
			AppStatus:       int32(req.AppStatus),
			UserId:          c.GetInt64("userId"),
			StartUpdateTime: req.StartUpdateTime,
		})
		if err != nil {
			response.SrvError(c, "应用更新失败", err.Error())
			return
		}
		response.Success(c, "应用更新成功", fmt.Sprintf("图片路径:%s,安装包路径:%s", way, url))

		// 只有FileUrl，没有文件的情况
	} else if req.FileUrl != "" && (file == nil || file.Filename == "") {

		url, err := untils.UploadOneCompressedFile(c, req.FileUrl)
		if err != nil {
			response.ParameterError(c, "上传安装包文件失败", err.Error())
			return
		}
		_, err = config.BaseClient.ApplicationUpdate(c, &__.ApplicationUpdateReq{
			Id:              req.Id,
			AppName:         req.AppName,
			AppSortOrder:    int32(req.AppSortOrder),
			AppPackageName:  req.AppPackageName,
			AppStatus:       int32(req.AppStatus),
			UserId:          c.GetInt64("userId"),
			StartUpdateTime: req.StartUpdateTime,
		})
		if err != nil {
			response.SrvError(c, "应用更新失败", err.Error())
			return
		}
		response.Success(c, "应用更新成功", fmt.Sprintf("安装包路径:%s", url))

		// 只有文件，没有FileUrl的情况
	} else if file != nil && file.Filename != "" && req.FileUrl == "" {

		get, err := file.Open()
		if err != nil {
			response.ParameterError(c, "打开文件失败", err.Error())
			return
		}
		defer get.Close() // 确保文件被关闭

		way, err := untils.QiNiuUpload(get, file)
		if err != nil {
			response.ParameterError(c, "上传文件失败", err.Error())
			return
		}
		// 保存到数据库
		_, err = config.BaseClient.ApplicationUpdateWithoutFile(c, &__.ApplicationUpdateWithoutFileReq{
			Id:              req.Id,
			AppName:         req.AppName,
			AppSortOrder:    int32(req.AppSortOrder),
			AppPackageName:  req.AppPackageName,
			AppStatus:       int32(req.AppStatus),
			UserId:          c.GetInt64("userId"),
			StartUpdateTime: req.StartUpdateTime,
		})
		if err != nil {
			response.SrvError(c, "应用更新失败", err.Error())
			return
		}
		response.Success(c, "应用更新成功", fmt.Sprintf("图片路径:%s", way))

		// 既没有文件也没有FileUrl的情况
	} else if (file == nil || file.Filename == "") && req.FileUrl == "" {
		// 保存到数据库
		_, err := config.BaseClient.ApplicationUpdateWithoutFile(c, &__.ApplicationUpdateWithoutFileReq{
			Id:              req.Id,
			AppName:         req.AppName,
			AppSortOrder:    int32(req.AppSortOrder),
			AppPackageName:  req.AppPackageName,
			AppStatus:       int32(req.AppStatus),
			UserId:          c.GetInt64("userId"),
			StartUpdateTime: req.StartUpdateTime,
		})
		if err != nil {
			response.SrvError(c, "应用更新失败", err.Error())
			return
		}
		response.Success(c, "应用更新成功", nil)
	}*/
}

// processApplicationUpdate 处理应用更新的核心逻辑
func processApplicationUpdate(c *gin.Context, req request.ApplicationUpdateReq, file *multipart.FileHeader) {
	var appIconPath string
	var packageUrl string
	var err error

	// 处理图片上传
	if file != nil {
		get, err := file.Open()
		if err != nil {
			response.ParameterError(c, "文件打开失败", err.Error())
			return
		}
		defer get.Close()

		appIconPath, err = untils.QiNiuUpload(get, file)
		if err != nil {
			response.ParameterError(c, "图标上传失败", err.Error())
			return
		}
	}

	// 处理安装包文件
	if req.FileUrl != "" {
		packageUrl, err = untils.UploadOneCompressedFile(c, req.FileUrl)
		if err != nil {
			response.ParameterError(c, "安装包上传失败", err.Error())
			return
		}
	}

	// 准备基础参数
	baseParams := &__.ApplicationUpdateWithoutFileReq{
		Id:              req.Id,
		AppName:         req.AppName,
		AppSortOrder:    int32(req.AppSortOrder),
		AppPackageName:  req.AppPackageName,
		AppStatus:       int32(req.AppStatus),
		UserId:          c.GetInt64("userId"),
		StartUpdateTime: req.StartUpdateTime,
	}

	// 根据不同情况调用不同的服务方法
	if file != nil && req.FileUrl != "" {
		// 有图标和安装包
		_, err = config.BaseClient.ApplicationUpdate(c, &__.ApplicationUpdateReq{
			Id:              req.Id,
			AppName:         req.AppName,
			AppSortOrder:    int32(req.AppSortOrder),
			AppIcon:         appIconPath,
			AppPackageName:  req.AppPackageName,
			AppStatus:       int32(req.AppStatus),
			UserId:          c.GetInt64("userId"),
			StartUpdateTime: req.StartUpdateTime,
		})
		if err != nil {
			response.SrvError(c, "应用更新失败", err.Error())
			return
		}
		response.Success(c, "应用更新成功", map[string]string{
			"图片路径":  appIconPath,
			"安装包路径": packageUrl,
		})
	} else if req.FileUrl != "" {
		// 只有安装包
		_, err = config.BaseClient.ApplicationUpdate(c, &__.ApplicationUpdateReq{
			Id:              req.Id,
			AppName:         req.AppName,
			AppSortOrder:    int32(req.AppSortOrder),
			AppPackageName:  req.AppPackageName,
			AppStatus:       int32(req.AppStatus),
			UserId:          c.GetInt64("userId"),
			StartUpdateTime: req.StartUpdateTime,
		})
		if err != nil {
			response.SrvError(c, "应用更新失败", err.Error())
			return
		}
		response.Success(c, "应用更新成功", map[string]string{
			"安装包路径": packageUrl,
		})
	} else {
		// 没有文件或只有图标
		_, err = config.BaseClient.ApplicationUpdateWithoutFile(c, baseParams)
		if err != nil {
			response.SrvError(c, "应用更新失败", err.Error())
			return
		}
		if file != nil {
			response.Success(c, "应用更新成功", map[string]string{
				"图片路径": appIconPath,
			})
		} else {
			response.Success(c, "应用更新成功", nil)
		}
	}
}

// ApplicationUpdateWithoutFile
// @Summary      更新应用版本(无文件)
// @Description  更新应用版本(无文件)
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.ApplicationUpdateReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/applicationUpdateWithoutFile [post]
// @Security TokenAuth
func ApplicationUpdateWithoutFile(c *gin.Context) {
	var req request.ApplicationUpdateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	// 保存到数据库
	_, err := config.BaseClient.ApplicationUpdateWithoutFile(c, &__.ApplicationUpdateWithoutFileReq{
		Id:              req.Id,
		AppName:         req.AppName,
		AppSortOrder:    int32(req.AppSortOrder),
		AppPackageName:  req.AppPackageName,
		AppStatus:       int32(req.AppStatus),
		UserId:          c.GetInt64("userId"),
		StartUpdateTime: req.StartUpdateTime,
	})
	if err != nil {
		response.SrvError(c, "应用更新失败", err.Error())
		return
	}
	response.Success(c, "应用更新成功", nil)
}

// ApplicationList
// @Summary      应用版本列表
// @Description  应用版本列表
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/applicationList [get]
// @Security TokenAuth
func ApplicationList(c *gin.Context) {
	resp, err := config.BaseClient.ApplicationList(c, &__.ApplicationListReq{})
	if err != nil {
		response.SrvError(c, "应用列表获取失败", err.Error())
		return
	}
	response.Success(c, "应用列表获取成功", resp)
}

// DriverAppAdd
// @Summary      司机端app添加
// @Description  司机端app添加
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.DriverAppAddReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/driverAppAdd [post]
// @Security TokenAuth
func DriverAppAdd(c *gin.Context) {
	var req request.DriverAppAddReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	url, err := untils.UploadOneCompressedFile(c, req.FileUrl)
	if err != nil {
		response.ParameterError(c, "上传安装包文件失败", err.Error())
		return
	}
	_, err = config.BaseClient.DriverAppAdd(c, &__.DriverAppAddReq{
		DriverAppVersion: req.DriverAppVersion,
		DriverAppName:    req.DriverAppName,
		UserId:           c.GetInt64("userId"),
		LaunchSystem:     req.LaunchSystem,
		IsForceUpdate:    req.IsForceUpdate,
		UpdateText:       req.UpdateText,
		StartUpdateTime:  req.StartUpdateTime,
	})
	if err != nil {
		response.SrvError(c, "司机端app添加失败", err.Error())
		return
	}
	response.Success(c, "司机端app添加成功", fmt.Sprintf("安装包路径:%s", url))
}

// DriverAppList
// @Summary      司机端app列表
// @Description  司机端app列表
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/driverAppList [get]
// @Security TokenAuth
func DriverAppList(c *gin.Context) {
	res, err := config.BaseClient.DriverAppList(c, &__.DriverAppListReq{})
	if err != nil {
		response.SrvError(c, "司机端app列表获取失败", err.Error())
		return
	}
	response.Success(c, "司机端app列表获取成功", res)
}

// DriverAppUpdate
// @Summary      司机端app修改
// @Description  司机端app修改
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.DriverAppUpdateReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/driverAppUpdate [post]
// @Security TokenAuth
func DriverAppUpdate(c *gin.Context) {
	var req request.DriverAppUpdateReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	var url string
	var err error
	if req.FileUrl != "" {
		url, err = untils.UploadOneCompressedFile(c, req.FileUrl)
		if err != nil {
			response.ParameterError(c, "上传安装包文件失败", err.Error())
			return
		}
	}

	_, err = config.BaseClient.DriverAppUpdate(c, &__.DriverAppUpdateReq{
		Id:               req.Id,
		DriverAppName:    req.DriverAppName,
		DriverAppVersion: req.DriverAppVersion,
		LaunchSystem:     req.LaunchSystem,
		IsForceUpdate:    req.IsForceUpdate,
		UpdateText:       req.UpdateText,
		UserId:           c.GetInt64("userId"),
		StartUpdateTime:  req.StartUpdateTime,
	})
	if err != nil {
		response.SrvError(c, "司机端app更新失败", err.Error())
		return
	}
	response.Success(c, "司机端app更新成功", url)
}

// PassengerAppAdd
// @Summary      乘客端app添加
// @Description  乘客端app添加
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.PassengerAppAddReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/passengerAppAdd [post]
// @Security TokenAuth
func PassengerAppAdd(c *gin.Context) {
	var req request.PassengerAppAddReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	url, err := untils.UploadOneCompressedFile(c, req.FileUrl)
	if err != nil {
		response.ParameterError(c, "上传安装包文件失败", err.Error())
		return
	}
	_, err = config.BaseClient.PassengerAppAdd(c, &__.PassengerAppAddReq{
		PassengerAppName:    req.PassengerAppName,
		PassengerAppVersion: req.PassengerAppVersion,
		UserId:              c.GetInt64("userId"),
		LaunchSystem:        req.LaunchSystem,
		IsForceUpdate:       req.IsForceUpdate,
		UpdateText:          req.UpdateText,
		StartUpdateTime:     req.StartUpdateTime,
	})
	if err != nil {
		response.SrvError(c, "乘客端app添加失败", err.Error())
		return
	}
	response.Success(c, "乘客端app添加成功", fmt.Sprintf("安装包路径:%s", url))
}

// PassengerAppList
// @Summary      乘客端app列表
// @Description  乘客端app列表
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/passengerAppList [post]
// @Security TokenAuth
func PassengerAppList(c *gin.Context) {
	res, err := config.BaseClient.PassengerAppList(c, &__.PassengerAppListReq{})
	if err != nil {
		response.SrvError(c, "乘客端app列表获取失败", err.Error())
		return
	}
	response.Success(c, "乘客端app列表获取成功", res)
}

// PassengerAppUpdate
// @Summary      乘客端app更新
// @Description  乘客端app更新
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.PassengerAppUpdateReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/passengerAppUpdate [post]
// @Security TokenAuth
func PassengerAppUpdate(c *gin.Context) {
	var req request.PassengerAppUpdateReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	var url string
	var err error
	if req.FileUrl != "" {
		url, err = untils.UploadOneCompressedFile(c, req.FileUrl)
		if err != nil {
			response.ParameterError(c, "上传安装包文件失败", err.Error())
			return
		}
	}

	_, err = config.BaseClient.PassengerAppUpdate(c, &__.PassengerAppUpdateReq{
		Id:                  req.Id,
		PassengerAppName:    req.PassengerAppName,
		PassengerAppVersion: req.PassengerAppVersion,
		LaunchSystem:        req.LaunchSystem,
		IsForceUpdate:       req.IsForceUpdate,
		UpdateText:          req.UpdateText,
		UserId:              c.GetInt64("userId"),
		StartUpdateTime:     req.StartUpdateTime,
	})
	if err != nil {
		response.SrvError(c, "乘客端app更新失败", err.Error())
		return
	}
	response.Success(c, "乘客端app更新成功", fmt.Sprintf("安装包路径:%s", url))
}

// CarAppUpdateAdd
// @Summary      车机端app添加
// @Description  车机端app添加
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.CarAppUpdateAddReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/carAppUpdateAdd [post]
// @Security TokenAuth
func CarAppUpdateAdd(c *gin.Context) {
	var req request.CarAppUpdateAddReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	url, err := untils.UploadOneCompressedFile(c, req.FileUrl)
	if err != nil {
		response.SrvError(c, "上传安装包文件失败", err.Error())
		return
	}
	_, err = config.BaseClient.CarAppUpdateAdd(c, &__.CarAppUpdateAddReq{
		CarAppName:      req.CarAppName,
		CarAppVersion:   req.CarAppVersion,
		VersionCode:     req.VersionCode,
		UpdateText:      req.UpdateText,
		UserId:          c.GetInt64("userId"),
		StartUpdateTime: req.StartUpdateTime,
	})
	if err != nil {
		response.SrvError(c, "车机端添加失败", err.Error())
		return
	}
	response.Success(c, "车机端添加成功", fmt.Sprintf("安装包路径:%s", url))
}

// CarAppUpdate
// @Summary      车机端app更新
// @Description  车机端app更新
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Param        base   body    request.CarAppUpdateReq    true  "参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/carAppUpdate [post]
// @Security TokenAuth
func CarAppUpdate(c *gin.Context) {
	var req request.CarAppUpdateReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	var url string
	var err error
	if req.FileUrl != "" {
		url, err = untils.UploadOneCompressedFile(c, req.FileUrl)
		if err != nil {
			response.SrvError(c, "上传安装包文件失败", err.Error())
			return
		}
	}
	_, err = config.BaseClient.CarAppUpdate(c, &__.CarAppUpdateReq{
		Id:              req.Id,
		CarAppName:      req.CarAppName,
		CarAppVersion:   req.CarAppVersion,
		VersionCode:     req.VersionCode,
		UpdateText:      req.UpdateText,
		UserId:          c.GetInt64("userId"),
		StartUpdateTime: req.StartUpdateTime,
	})
	if err != nil {
		response.SrvError(c, "车机端更新失败", err.Error())
		return
	}

	response.Success(c, "车机端更新成功", fmt.Sprintf("安装包路径:%s", url))
}

// CarAppUpdateList
// @Summary      车机端app更新列表
// @Description  车机端app更新列表
// @Tags         基础信息模块
// @Accept       json
// @Produce      json
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /base/carAppUpdateList [post]
// @Security TokenAuth
func CarAppUpdateList(c *gin.Context) {
	res, err := config.BaseClient.CarAppUpdateList(c, &__.CarAppUpdateListReq{})
	if err != nil {
		response.SrvError(c, "车机端更新列表获取失败", err.Error())
		return
	}
	response.Success(c, "车机端更新列表获取成功", res)

}

func UploadToQiNiu(c *gin.Context) {
	file, err := c.FormFile("image")
	if err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	get, err := file.Open()
	if err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	url, err := untils.QiNiuUpload(get, file)
	if err != nil {
		response.ParameterError(c, "上传文件失败", err.Error())
		return
	}
	response.Success(c, "上传文件成功", url)
}

func OrganizationAdd(c *gin.Context) {
	var req request.OrganizationAddReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	_, err := config.BaseClient.OrganizationAdd(c, &__.OrganizationAddReq{
		OrganizationName:   req.OrganizationName,
		OrganizationCode:   req.OrganizationCode,
		AdministrativeCode: req.AdministrativeCode,
		ServiceAddress:     req.ServiceAddress,
		HeaderName:         req.HeaderName,
		HeaderMobile:       req.HeaderMobile,
		ManagerName:        req.ManagerName,
		ManagerMobile:      req.ManagerMobile,
		OrganizationMobile: req.OrganizationMobile,
		MailingAddress:     req.MailingAddress,
		RegisterTime:       req.RegisterTime,
		Status:             req.Status,
	})
	if err != nil {
		response.SrvError(c, "组织添加失败", err.Error())
		return
	}
	response.Success(c, "组织添加成功", nil)
}

func OrganizationUpdate(c *gin.Context) {
	var req request.OrganizationUpdateReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	_, err := config.BaseClient.OrganizationUpdate(c, &__.OrganizationUpdateReq{
		Id:                 req.Id,
		OrganizationName:   req.OrganizationName,
		OrganizationCode:   req.OrganizationCode,
		AdministrativeCode: req.AdministrativeCode,
		ServiceAddress:     req.ServiceAddress,
		HeaderName:         req.HeaderName,
		HeaderMobile:       req.HeaderMobile,
		ManagerName:        req.ManagerName,
		ManagerMobile:      req.ManagerMobile,
		OrganizationMobile: req.OrganizationMobile,
		MailingAddress:     req.MailingAddress,
		RegisterTime:       req.RegisterTime,
		Status:             req.Status,
	})
	if err != nil {
		response.SrvError(c, "组织更新失败", err.Error())
		return
	}
	response.Success(c, "组织更新成功", nil)
}

func BusinessLicenseInformationAdd(c *gin.Context) {
	var req request.BusinessLicenseInformationAddReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	_, err := config.BaseClient.BusinessLicenseInformationAdd(c, &__.BusinessLicenseInformationAddReq{
		OrganizationName:     req.OrganizationName,
		AdministrativeCode:   req.AdministrativeCode,
		PermitCode:           req.PermitCode,
		BusinessScope:        req.BusinessScope,
		SendOrganizationName: req.SendOrganizationName,
		LicenseStartDate:     req.LicenseStartDate,
		LicenseEndDate:       req.LicenseEndDate,
		IssuanceDate:         req.IssuanceDate,
		Status:               req.Status,
	})
	if err != nil {
		response.SrvError(c, "经营许可证信息添加失败", err.Error())
		return
	}
	response.Success(c, "经营许可证信息添加成功", nil)
}

func BusinessLicenseInformationUpdate(c *gin.Context) {
	var req request.BusinessLicenseInformationUpdateReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	_, err := config.BaseClient.BusinessLicenseInformationUpdate(c, &__.BusinessLicenseInformationUpdateReq{
		Id:                   uint32(req.Id),
		OrganizationName:     req.OrganizationName,
		AdministrativeCode:   req.AdministrativeCode,
		PermitCode:           req.PermitCode,
		BusinessScope:        req.BusinessScope,
		SendOrganizationName: req.SendOrganizationName,
		LicenseStartDate:     req.LicenseStartDate,
		LicenseEndDate:       req.LicenseEndDate,
		IssuanceDate:         req.IssuanceDate,
		Status:               req.Status,
	})
	if err != nil {
		response.SrvError(c, "经营许可证信息更新失败", err.Error())
		return
	}
	response.Success(c, "经营许可证信息更新成功", nil)
}
